package test;


import org.newdawn.slick.*;
import org.newdawn.slick.openal.SoundStore;
import soundly.ManagedSound;
import soundly.Soundly;
import soundly.XSound;

/**
 *
 * @author Matt
 */
public class SourceDebugOverlay {
    
    public static final int PADDING = 50;
    public static final int SOURCE_PADDING = 5;
    public static final int DOT_SIZE = 10;
    
    static Color colorBG = new Color(0f, 0f, 0f, 1f);
    static Color color1 = new Color(246, 219, 184);
    static Color color2 = new Color(191, 182, 164);
    static Color color3 = new Color(140, 141, 144);
    static Color color4 = new Color(107, 93, 105);
    static Color color5 = new Color(79, 44, 64);
    
    static Color overlayAlpha = new Color(1f, 1f, 1f, 0.75f);
    
    static int overlayWidth;
    static int overlayHeight;
    
    static Graphics gTarget;
    static Image buffer;
    
    public static void init(GameContainer container) throws SlickException {
        overlayWidth = (int)(container.getWidth()/2f-PADDING);
        overlayHeight = (int)(container.getHeight()-PADDING);
        buffer = new Image(overlayWidth, overlayHeight);
        gTarget = buffer.getGraphics();
    }
    
    public static void render(GameContainer container, Graphics g) {
        //render to offscreen image, then draw to display
        int sourceCount = SoundStore.get().getSourceCount();
        Soundly sound = Soundly.get();
        
        g.setColor(Color.white);
        int sx = 15;
        int sy = PADDING+15;
        String plr = sourceCount>1 ? "s":"";
        g.drawString(sourceCount+" sound source"+plr, sx, sy);
        sy+=15;
        for (int i=0; i<sourceCount; i++) {
            int sourceID = i;
            int src = sound.getSource(i);
            XSound source = sound.getSoundAt(i);
            
            String msg = "--";
            String r = i<sound.getMusicTrackCount() ? "[music] " : "";
            String state = "";
            int sid = sound.getSource(sourceID);
            if (source!=null) {
                msg = source.toString();
                state = "idle    - ";
                
                if (source.isPlaying())
                    state = "playing - ";
                else if (source.isPaused())
                    state = "paused  - ";
                state += "(layer "+source.getLayer()+") ";
            } else if (i==0) {
                if (SoundStore.get().isMusicOn() && SoundStore.get().isMusicPlaying())
                    msg = "playing - [Slick music]";
            }
            String spacer = i<9&&sourceCount>9?" ":"";
            g.drawString("Source (id: "+sid+") "+spacer+(i+1)+": "+r+state+msg, sx, sy);
            sy+=15;
        }
        
        gTarget.setAntiAlias(false);
        gTarget.flush();
        
        float cx = PADDING;
        float cy = container.getHeight()/2f-overlayHeight/2f;
        buffer.draw(cx, cy);
    }
}
